<template>
  <div class="all">
    <el-table :data="pagination.records" border>
      <el-table-column fixed="left" prop="teacherName" label="姓名" width="180"></el-table-column>
      <el-table-column prop="institute" label="学院" width="200"></el-table-column>
      <el-table-column prop="sex" label="性别" width="120"></el-table-column>
      <el-table-column prop="tel" label="联系方式" width="120"></el-table-column>
      <el-table-column prop="email" label="密码" width="120"></el-table-column>
      <el-table-column prop="cardId" label="身份证号" width="120"></el-table-column>
      <el-table-column prop="type" label="职称" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template v-slot:default="scope">
          <el-button @click="checkTeacher(scope.row.teacherId)" type="primary" size="small">编辑</el-button>
          <el-button @click="deleteTeacher(scope.row.teacherId)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[6, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page">
    </el-pagination>

    <el-dialog
      title="编辑教师信息"
      v-model="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="form.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="学院">
            <el-input v-model="form.institute"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="form.sex"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="form.tel"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.pwd"></el-input>
          </el-form-item>
          <el-form-item label="身份证号">
            <el-input v-model="form.cardId"></el-input>
          </el-form-item>
          <el-form-item label="职称">
            <el-input v-model="form.type"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitTeacherInfo">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';

export default {
  name: 'TeacherManagement',
  setup() {
    const pagination = reactive({
      current: 1,
      total: null,
      size: 6,
      records: []
    });

    const dialogVisible = ref(false);
    const form = reactive({});

    const { proxy } = getCurrentInstance();

    const fetchTeacherInfo = () => {
      proxy.$axios(`/api/teachers/${pagination.current}/${pagination.size}`)
        .then(res => {
          pagination.records = res.data.data.records;
          pagination.total = res.data.data.total;
        })
        .catch(console.log);
    };

    const handleSizeChange = (val) => {
      pagination.size = val;
      fetchTeacherInfo();
    };

    const handleCurrentChange = (val) => {
      pagination.current = val;
      fetchTeacherInfo();
    };

    const checkTeacher = (teacherId) => {
      dialogVisible.value = true;
      proxy.$axios(`/api/teacher/${teacherId}`)
        .then((res) => {
          form.value = res.data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    };

    const deleteTeacher = (teacherId) => {
      proxy.$confirm('确定删除当前教师吗？删除后无法恢复', 'Warning', {
        confirmButtonText: '确定删除',
        cancelButtonText: '算了,留着吧',
        type: 'danger'
      }).then(() => {
        proxy.$axios({
          url: `/api/teacher/${teacherId}`,
          method: 'delete',
        }).then(() => {
          fetchTeacherInfo();
        });
      }).catch(() => {});
    };

    const submitTeacherInfo = () => {
      dialogVisible.value = false;
      proxy.$axios({
        url: '/api/teacher',
        method: 'put',
        data: { ...form },
      }).then(res => {
        if (res.data.code === 200) {
          proxy.$message({
            message: '更新成功',
            type: 'success',
          });
          fetchTeacherInfo();
        }
      });
    };

    const handleClose = (done) => {
      proxy.$confirm('确认关闭？')
        .then(() => done())
        .catch(() => {});
    };

    onMounted(() => {
      fetchTeacherInfo();
    });

    return {
      pagination,
      dialogVisible,
      form,
      fetchTeacherInfo,
      handleSizeChange,
      handleCurrentChange,
      checkTeacher,
      deleteTeacher,
      submitTeacherInfo,
      handleClose,
    };
  }
};
</script>

<style lang="less" scoped>
.all {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table tr {
    background-color: #dd5862 !important;
  }
}
.el-table .warning-row {
  background: #000 !important;
}

.el-table .success-row {
  background: #dd5862;
}
</style>
